package com.demo.blog.model;

import com.demo.blog.common.Constants;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-07-24
 * Time: 11:11
 */
@Data
public class Result<T> {
    private int code;//200-成功  -1 失败
    private String errorMsg;
    private T data;

    /**
     * 业务执⾏成功时返回的⽅法
     *
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setCode(Constants.RESULT_SUCCESS);
        result.setData(data);
        result.setErrorMsg("");
        return result;
    }

    /**
     * 业务执⾏失败时返回的⽅法
     *
     * @param
     * @return
     */
    public static <T> Result<T> fail(String errorMsg) {
        Result result = new Result();
        result.setCode(Constants.RESULT_FAIL);
        result.setErrorMsg(errorMsg);
        return result;
    }

    public static <T> Result<T> fail(String errorMsg, T data) {
        Result result = new Result();
        result.setCode(Constants.RESULT_FAIL);
        result.setErrorMsg(errorMsg);
        result.setData(data);
        return result;
    }
}
